#include "led_bee_fan_widget.h"
#include "ui_led_bee_fan_widget.h"
#include "fsmpLed.h"
#include "fsmpBeeper.h"
#include "fsmpVibrator.h"
#include <QDebug>
#include <string.h>
#include <QByteArray>
#include <QString>
#include <QJsonDocument>
#include <QJsonObject>

led_bee_fan_Widget::led_bee_fan_Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::led_bee_fan_Widget)
{
    ui->setupUi(this);

    led = new fsmpLeds;

    bee = new fsmpBeeper;

    fan = new fsmpFan;

    vib = new fsmpVibrator;

    onlineService();

}

led_bee_fan_Widget::~led_bee_fan_Widget()
{
    delete ui;
    led->~fsmpLeds();
    bee->~fsmpBeeper();
    fan->~fsmpFan();
    vib->~fsmpVibrator();
}
//按钮１：硬件
void led_bee_fan_Widget::on_pushButton_led1_clicked()
{

    char* text = ui->pushButton_led1->text().toLatin1().data();
    if(strcmp(text,"on") == 0)
    {
        ui->label_led1->setStyleSheet("border-image: url(:/group4/image/picture/led_on.png);");
        bool flag = led->on(led->LED1);
        publishLed1Msg(true,0);
        if(flag)
        {
            ui->pushButton_led1->setText("off");
        }
        else
        {
            qDebug() << "on is false";
        }
    }
    else if(strcmp(text,"off") == 0)
    {
        ui->label_led1->setStyleSheet("border-image: url(:/group4/image/picture/led_off.png);");
        bool flag = led->off(led->LED1);
        publishLed1Msg(false,0);
        if(flag)
        {
            ui->pushButton_led1->setText("on");
        }
        else
        {
            qDebug() << "off is false";
        }
    }
    else
    {
        qDebug() <<"Led1 is false";
    }
}
//按钮２：硬件
void led_bee_fan_Widget::on_pushButton_led2_clicked()
{
    char* text = ui->pushButton_led2->text().toLatin1().data();
    if(strcmp(text,"on") == 0)
    {
        ui->label_led2->setStyleSheet("border-image: url(:/group4/image/picture/led_on.png);");
        bool flag = led->on(led->LED2);
        publishLed2Msg(true,1);
        if(flag)
        {
            ui->pushButton_led2->setText("off");
        }
        else
        {
            qDebug() << "on is false";
        }
    }
    else if(strcmp(text,"off") == 0)
    {
        ui->label_led2->setStyleSheet("border-image: url(:/group4/image/picture/led_off.png);");
        bool flag = led->off(led->LED2);
        publishLed2Msg(false,1);
        if(flag)
        {
            ui->pushButton_led2->setText("on");
        }
        else
        {
            qDebug() << "off is false";
        }
    }
    else
    {
        qDebug() <<"Led2 is killed";
    }
}
//按钮3：硬件
void led_bee_fan_Widget::on_pushButton_led3_clicked()
{
    char* text = ui->pushButton_led3->text().toLatin1().data();
    if(strcmp(text,"on") == 0)
    {
        ui->label_led3->setStyleSheet("border-image: url(:/group4/image/picture/led_on.png);");
        bool flag = led->on(led->LED3);
        publishLed3Msg(true,2);
        if(flag)
        {
            ui->pushButton_led3->setText("off");
        }
        else
        {
            qDebug() << "on is false";
        }
    }
    else if(strcmp(text,"off") == 0)
    {
        ui->label_led3->setStyleSheet("border-image: url(:/group4/image/picture/led_off.png);");
        bool flag = led->off(led->LED3);
        publishLed3Msg(false,2);
        if(flag)
        {
            ui->pushButton_led3->setText("on");
        }
        else
        {
            qDebug() << "off is false";
        }
    }
    else
    {
        qDebug() <<"Led3 is killed";
    }
}
//蜂鸣器：硬件
void led_bee_fan_Widget::on_pushButton_bee_clicked()
{
    char *text = ui->pushButton_bee->text().toLatin1().data();
    if(strcmp(text,"on") == 0)
    {
        ui->label_bee->setStyleSheet("border-image: url(:/group4/image/picture/bee_on.png);");
        //蜂鸣器叫
        bee->setRate(1000);
        bool flag = bee->start();
        //马达动
//        vib->setParameter(5000,2000);
        vib->setParameter();
        vib->start();
        publishBeeperMsg(true,0);
        if(flag)
        {
            ui->pushButton_bee->setText("off");
        }
        else
        {
            qDebug() << "on is false";
        }
    }
    else if(strcmp(text,"off") == 0)
    {
        ui->label_bee->setStyleSheet("border-image: url(:/group4/image/picture/bee_off.png);");
        //蜂鸣器不叫
        bee->setRate(0);
        bool flag = bee->stop();
        //马达停止
        vib->stop();
        publishBeeperMsg(false,0);
        if(flag)
        {
            ui->pushButton_bee->setText("on");
        }
        else
        {
            qDebug() << "off is false";
        }
    }
    else
    {
        qDebug() << "bee is killed";
    }
}
//风扇：硬件
void led_bee_fan_Widget::on_pushButton_fan_clicked()
{
    char *text = ui->pushButton_fan->text().toLatin1().data();
    if(strcmp(text,"on") == 0)
    {
        fan->setSpeed(200);
        fan->start();
        publishFanMsg(true,0);
        ui->label_fan->setStyleSheet("border-image: url(:/group4/image/picture/fan_on.png);");
        ui->pushButton_fan->setText("off");
    }
    else if(strcmp(text,"off") == 0)
    {
        fan->stop();
        publishFanMsg(false,0);
        ui->label_fan->setStyleSheet("border-image: url(:/group4/image/picture/fan_off.png);");
        ui->pushButton_fan->setText("on");
    }
    else
    {
        qDebug() << "fan is killed";
    }
}

//处理接受到的消息
void led_bee_fan_Widget::Receive(QByteArray buf)
{
    //    qDebug() << buf;
    QJsonDocument d = QJsonDocument::fromJson(buf);
    QJsonObject obj = d.object();
    if(!obj.isEmpty())
    {
        if(obj.contains("lamp"))
        {
            if(obj.value("id").toInt() == 0)
            {
                if(obj.value("lamp").toBool())
                {

                    ui->label_led1->setStyleSheet("border-image: url(:/group4/image/picture/led_on.png);");
                    led->on(led->LED1);
                    ui->pushButton_led1->setText("off");
                }else
                {
                    ui->label_led1->setStyleSheet("border-image: url(:/group4/image/picture/led_off.png);");
                    led->off(led->LED1);
                    ui->pushButton_led1->setText("on");

                }
            }
            if(obj.value("id").toInt() == 1)
            {
                led->on(led->LED2);
                if(obj.value("lamp").toBool())
                {
                    ui->pushButton_led2->setText("off");
                }else
                {
                    ui->pushButton_led2->setText("on");
                }
            }
            if(obj.value("id").toInt() == 2)
            {
                led->on(led->LED3);
                if(obj.value("lamp").toBool())
                {
                    ui->pushButton_led3->setText("off");
                }else
                {
                    ui->pushButton_led3->setText("on");
                }
            }
        }
        else if(obj.contains("fan"))
        {
            if(obj.value("fan").toBool())
            {
                ui->label_fan->setStyleSheet("border-image: url(:/group4/image/picture/fan_on.png);");
                ui->pushButton_fan->setText("off");
                fan->setSpeed(200);
                fan->start();
            }
            else
            {
                ui->label_fan->setStyleSheet("border-image: url(:/group4/image/picture/fan_off.png);");
                ui->pushButton_fan->setText("on");
                fan->stop();
            }
        }
        else if(obj.contains("alarm"))
        {
            if(obj.value("alarm").toBool())
            {
                ui->label_bee->setStyleSheet("border-image: url(:/group4/image/picture/bee_on.png);");
                bee->setRate(1000);
                ui->pushButton_bee->setText("off");
                bee->start();
            }
            else
            {
                ui->label_bee->setStyleSheet("border-image: url(:/group4/image/picture/bee_off.png);");
                bee->setRate(0);
                bee->stop();
                ui->pushButton_bee->setText("on");
            }
        }
    }
}

void led_bee_fan_Widget::on_backButton_clicked()
{
    this->close();
}

